
package cup.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import cup.core.boot.ctrl.CupController;
import cup.core.log.exception.ServiceException;
import cup.core.secure.CupUser;
import cup.core.secure.annotation.PreAuth;
import cup.core.secure.utils.AuthUtil;
import cup.core.tool.constant.RoleConstant;
import cup.modules.system.entity.AppVersions;
import cup.modules.system.service.IAppVersionsService;
import cup.modules.system.vo.AppVersionsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.validation.Valid;

import cup.core.mp.support.Condition;
import cup.core.mp.support.Query;
import cup.core.tool.api.R;
import cup.core.tool.utils.Func;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.Date;
import java.util.List;


/**
 * app版本管理 控制器
 *
 * @author BladeX
 * @since 2022-07-06
 */
@RestController
@AllArgsConstructor
@RequestMapping("base/appversions")
@Api(value = "app版本管理", tags = "app版本管理接口")
public class AppVersionsController extends CupController {

	private IAppVersionsService appVersionsService;


	/**
	 * 自定义分页 app版本管理
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "分页", notes = "传入appVersions")
	public R<IPage<AppVersions>> page(AppVersionsVO appVersions, Query query) {
		IPage<AppVersions> pages = appVersionsService.page(Condition.getPage(query), Condition.getQueryWrapper(appVersions));
		return R.data(pages);
	}

	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入appVersions")
	public R<IPage<AppVersionsVO>> list(AppVersionsVO appVersions, Query query){
		IPage<AppVersionsVO> pages = appVersionsService.selectAppVersionsPage(Condition.getPage(query), appVersions);
		return R.data(pages);
	}

	/**
	 * 新增 app版本管理
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "新增", notes = "传入appVersions")
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	public R save(@Valid @RequestBody AppVersions appVersions) {
		CupUser user = AuthUtil.getUser();
		appVersions.setCreateUser(user.getUserId());
		appVersions.setCreateTime(new Date());
		appVersions.setStatus(1);
		//如果是最新版本 默认把其他数据全修改为0
		if (appVersions.getIsNew().equals(1)) {
			appVersionsService.update(new UpdateWrapper<AppVersions>().set("is_new", 0));
		}
		return R.status(appVersionsService.save(appVersions));
	}

	/**
	 * 修改为最新版本
	 */
	@PostMapping("/new")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "修改为最新版本", notes = "传入id")
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	public R newVersion(@ApiParam(value = "主键id", required = true) @RequestParam Long id) {
		AppVersions byId = appVersionsService.getById(id);
		if (null == byId) {
			throw new ServiceException("未查询到版本主键id信息:[" + id + "]");
		}
		appVersionsService.update(new UpdateWrapper<AppVersions>().set("is_new", 0));
		byId.setIsNew(1);
		appVersionsService.updateById(byId);
		return R.status(true);
	}

	/**
	 * 查询最新版本
	 */
	@PostMapping("/findNew")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "查询最新版本")
	public R findNew() {
		AppVersions newVersion = appVersionsService.getOne(new QueryWrapper<AppVersions>().eq("is_new", 1));
		return R.data(newVersion);
	}

	/**
	 * 删除 app版本管理
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		List<Long> longs = Func.toLongList(ids);
		List<AppVersions> appVersions = appVersionsService.listByIds(longs);
		if (!CollectionUtils.isEmpty(appVersions) && appVersions.stream().anyMatch(x -> x.getIsNew().equals(1))) {
			throw new ServiceException("最新的版本不能被删除,请新增额外最新版本后再删除");
		}
		return R.status(appVersionsService.removeByIds(longs));
	}
}
